package com.tledu.email.service.impl;

import java.util.List;

import com.tledu.common.core.domain.entity.SysDept;
import com.tledu.common.core.domain.entity.SysUser;
import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.system.service.ISysDeptService;
import com.tledu.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tledu.email.mapper.EmailWriteMapper;
import com.tledu.email.domain.EmailWrite;
import com.tledu.email.service.IEmailWriteService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 写信Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class EmailWriteServiceImpl implements IEmailWriteService {
    @Resource
    private EmailWriteMapper emailWriteMapper;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询写信
     *
     * @param id 写信主键
     * @return 写信
     */
    @Override
    public EmailWrite selectEmailWriteById(Long id) {
        return emailWriteMapper.selectEmailWriteById(id);
    }

    /**
     * 查询写信列表
     *
     * @param emailWrite 写信
     * @return 写信
     */
    @Override
    public List<EmailWrite> selectEmailWriteList(EmailWrite emailWrite) {
        return emailWriteMapper.selectEmailWriteList(emailWrite);
    }

    /**
     * 新增写信
     *
     * @param emailWrite 写信
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEmailWrite(EmailWrite emailWrite) {
        // 获取收件人名称
        SysUser user = userService.selectUserById(emailWrite.getRecipientsId());
        String userName = user.getUserName();
        emailWrite.setRecipientsName(userName);
        // 获取部门id
        emailWrite.setDeptId(SecurityUtils.getDeptId());
        // 部门名称
        SysDept dept = deptService.selectDeptById(SecurityUtils.getDeptId());
        emailWrite.setDeptName(dept.getDeptName());
        // 获取当前用户id
        emailWrite.setCreateId(SecurityUtils.getUserId());
        // 获取发件人名称
        emailWrite.setCreateBy(SecurityUtils.getUsername());
        // 创建事件
        emailWrite.setCreateTime(DateUtils.getNowDate());
        emailWrite.setCreateTime(DateUtils.getNowDate());
        EmailWrite emailWrite1 = new EmailWrite();
        BeanUtils.copyProperties(emailWrite, emailWrite1);
        // 发件箱
        emailWrite.setSymbol(2L);
        // 收件箱
        emailWrite1.setSymbol(1L);

        int n = emailWriteMapper.insertEmailWrite(emailWrite);
        int m = emailWriteMapper.insertEmailWrite(emailWrite1);
        if (n > 0 && m > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改写信
     *
     * @param emailWrite 写信
     * @return 结果
     */
    @Override
    public int updateEmailWrite(EmailWrite emailWrite) {
        emailWrite.setUpdateTime(DateUtils.getNowDate());
        return emailWriteMapper.updateEmailWrite(emailWrite);
    }

    /**
     * 批量删除写信
     *
     * @param ids 需要删除的写信主键
     * @return 结果
     */
    @Override
    public int deleteEmailWriteByIds(Long[] ids) {
        return emailWriteMapper.deleteEmailWriteByIds(ids);
    }

    /**
     * 删除写信信息
     *
     * @param id 写信主键
     * @return 结果
     */
    @Override
    public int deleteEmailWriteById(Long id) {
        return emailWriteMapper.deleteEmailWriteById(id);
    }

    // 存为草稿
    @Override
    public int saveAsDraft(EmailWrite emailWrite) {
        // 获取收件人名称
        SysUser user = userService.selectUserById(emailWrite.getRecipientsId());
        String userName = user.getUserName();
        emailWrite.setRecipientsName(userName);
        // 获取部门id
        emailWrite.setDeptId(SecurityUtils.getDeptId());
        // 部门名称
        SysDept dept = deptService.selectDeptById(SecurityUtils.getDeptId());
        emailWrite.setDeptName(dept.getDeptName());
        // 获取用户id
        emailWrite.setCreateId(SecurityUtils.getUserId());
        // 获取用户name
        emailWrite.setCreateBy(SecurityUtils.getUsername());
        // 创建事件
        emailWrite.setCreateTime(DateUtils.getNowDate());
        // 设置symbol=0草稿类型
        emailWrite.setSymbol(0L);
        return emailWriteMapper.saveAsDraft(emailWrite);
    }
}
